/*
 * Created by JFormDesigner on Sun Jun 20 18:03:39 GMT+08:00 2021
 */

package com.bk.view;

import com.bk.dao.BookTypeDao;
import com.bk.util.ToolUtil;

import java.awt.*;
import java.awt.event.*;
import java.sql.SQLException;
import javax.swing.*;
import javax.swing.GroupLayout;

/**
 * @author Ayi
 */
// 添加图书类型类
public class addBookTypeFrame extends JFrame {
    public addBookTypeFrame() {
        initComponents();
    }

    // 添加图书类型，监听
    private void button1ActionPerformed(ActionEvent e) {
        // TODO add your code here
        String btName = textField2.getText();
        if (new ToolUtil().judgeIdOrName(btName.trim())) {
            JOptionPane.showMessageDialog(null, "类型名不可以是纯数字！");
            return;
        }
//        int btInventory = (int)spinner1.getValue();
        try {
            if (new BookTypeDao().addBookType(btName)){
                JOptionPane.showMessageDialog(null, "添加成功");
                dispose();
            }else {
                JOptionPane.showMessageDialog(null, "添加失败，请检查类型名是否为空或已存在");
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    private void textField2KeyTyped(KeyEvent e) {
        // TODO add your code here
        // 文本框监听，限制输入长度
        String s = textField2.getText();
        if (s.length() > 7) {
            e.consume();
        }
    }


    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        // Generated using JFormDesigner Evaluation license - unknown
        label2 = new JLabel();
        textField2 = new JTextField();
        button1 = new JButton();

        //======== this ========
        setTitle("\u65b0\u589e\u56fe\u4e66\u7c7b\u578b");
        setResizable(false);
        Container contentPane = getContentPane();

        //---- label2 ----
        label2.setText("\u65b0\u589e\u7c7b\u578b\u540d");
        label2.setFont(new Font("\u5fae\u8f6f\u96c5\u9ed1", Font.PLAIN, 14));

        //---- textField2 ----
        textField2.addKeyListener(new KeyAdapter() {
            @Override
            public void keyTyped(KeyEvent e) {
                textField2KeyTyped(e);
            }
        });

        //---- button1 ----
        button1.setText("\u786e\u5b9a");
        button1.setBackground(Color.white);
        button1.addActionListener(e -> button1ActionPerformed(e));

        GroupLayout contentPaneLayout = new GroupLayout(contentPane);
        contentPane.setLayout(contentPaneLayout);
        contentPaneLayout.setHorizontalGroup(
            contentPaneLayout.createParallelGroup()
                .addGroup(contentPaneLayout.createSequentialGroup()
                    .addGap(42, 42, 42)
                    .addComponent(label2)
                    .addGap(18, 18, 18)
                    .addComponent(textField2, GroupLayout.PREFERRED_SIZE, 113, GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(30, Short.MAX_VALUE))
                .addGroup(GroupLayout.Alignment.TRAILING, contentPaneLayout.createSequentialGroup()
                    .addContainerGap(69, Short.MAX_VALUE)
                    .addComponent(button1, GroupLayout.PREFERRED_SIZE, 142, GroupLayout.PREFERRED_SIZE)
                    .addGap(62, 62, 62))
        );
        contentPaneLayout.setVerticalGroup(
            contentPaneLayout.createParallelGroup()
                .addGroup(contentPaneLayout.createSequentialGroup()
                    .addGap(28, 28, 28)
                    .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(label2)
                        .addComponent(textField2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                    .addGap(26, 26, 26)
                    .addComponent(button1)
                    .addContainerGap(29, Short.MAX_VALUE))
        );
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    // Generated using JFormDesigner Evaluation license - unknown
    private JLabel label2;
    private JTextField textField2;
    private JButton button1;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
